﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Kokomo.Mirror.CliMetadata
{
	public class MetadataTypedList : TypedList<IMetadataRow>
	{
		public MetadataTypedList(
			Type rowType,
			IList<IMetadataRow> rows,
			MetadataContainer container
			)
			: base(rows)
		{
			if ((object)rowType == null) throw new ArgumentNullException("rowType");
			if (container == null) throw new ArgumentNullException("container");

			this.RowType = rowType;
			this.Container = container;
		}

		public Type RowType { get; private set; }
		public MetadataContainer Container { get; private set; }

		protected override ListItemDescriptor<IMetadataRow> CreateDescriptor(
			IMetadataRow item
			)
		{
			return new MetadataRowDescriptor(item, this.Container, this);
		}

		protected override System.ComponentModel.PropertyDescriptorCollection GetPropertiesCore(Attribute[] attributes)
		{
			List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
			properties.Add(new RowNumberPropertyDescriptor(this));
			properties.AddRange(MetadataRowDescriptor.GetPropertiesForType(this.RowType, this.Container).OfType<PropertyDescriptor>());
			return new PropertyDescriptorCollection(properties.ToArray());
		}
	}
}
